package com.fpmsa.officeweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.core.exception.BizExceptionEnum;
import com.fpmsa.officeweb.core.exception.GunsException;
import com.fpmsa.officeweb.core.util.ToolUtil;
import com.fpmsa.officeweb.model.Job;
import com.fpmsa.officeweb.service.IJobService;
import com.fpmsa.officeweb.service.OfficeWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Huang Yuehong
 * @Description: 官网招聘控制器
 * @Date: 2018/7/1 14:29
 */

@Controller
@RequestMapping(value = "/job")
public class JobController {

    @Autowired
    private OfficeWebService officeWebService;

    @Autowired
    private IJobService jobService;

    private String PREFIX = "/templates/";

    @RequestMapping("")
    public String jobList(HttpServletRequest request, Model model){

        // 获取新闻列表（类别为“协会新闻”）,按时间降序排列
        EntityWrapper<Job> wrapper = new EntityWrapper<>();
        wrapper.eq("is_del",0);
        wrapper.orderBy("create_time",false);
        List<Job> jobList = jobService.selectList(wrapper);

        List<Map<String,Object>> jobMapList = jobService.formatDate(jobList,"yyyy-MM-dd");
        model.addAttribute("jobMapList",jobMapList);

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "job.html";
    }

    @RequestMapping("/jobDetail/{jobId}")
    public String jobList(@PathVariable("jobId") Long jobId,HttpServletRequest request, Model model){
        if (ToolUtil.isEmpty(jobId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }

        Job job = jobService.selectById(jobId);
        if (job != null){
            model.addAttribute("jobname",job.getName());
            model.addAttribute("requirement",job.getRequirement());
            model.addAttribute("duty",job.getDuty());
            model.addAttribute("welfare",job.getWelfare());
            model.addAttribute("salary",job.getSalary());
        }

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "jobDetail.html";
    }
}
